<%--***************************************
*
*财付通
* 
***************************************--%>
<%@ page import="
	java.util.*,
	java.math.*,
	java.math.BigDecimal,
	com.probiz.estore.payment.tenpay.PayResponseHandler,
	com.probiz.estore.Constants,
	com.probiz.estore.common.helper.ConfigUtil,
	com.probiz.estore.order.model.OnlinePaymentResponse,
	com.probiz.estore.common.model.system.PaymentGateway,
	com.probiz.estore.system.service.PaymentGatewayManager,
	com.probiz.estore.common.service.OrderService,
	org.springframework.web.context.support.WebApplicationContextUtils,
	org.springframework.web.context.WebApplicationContext"
	pageEncoding="UTF-8"%>
<%
	WebApplicationContext cxt=WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext());
	PaymentGatewayManager paymentGatewayManager=(PaymentGatewayManager)cxt.getBean("paymentGatewayManager");
	PaymentGateway paymentGateway=paymentGatewayManager.getByPaymentGatewayCode("tenpay");
	HashMap paramMap = paymentGateway.getConfigData();
	
	Short flag = Constants.PAYMENT_HISTORY_FLAG_FAILURE;
	//平台商密钥
	String key = paramMap.get("key").toString();
	
	//创建MediPayResponseHandler实例
	PayResponseHandler resHandler = new PayResponseHandler(request, response);
	
	//支付后的商户支付结果展示页面
	String show_url = ConfigUtil.getInstance().getStoreFrontSiteUrl() + "/system/payment/showPayResult.html?flag=1";
	
	resHandler.setKey(key);
	
	//判断签名
	if(resHandler.isTenpaySign()) {
		//交易单号
		String transaction_id = resHandler.getParameter("transaction_id");
		//金额,以分为单位
		String total_fee = resHandler.getParameter("total_fee");
		BigDecimal amount_total_fee = new BigDecimal(0);
		if (null!=total_fee && !"".equals(total_fee) ) {
			try {//may be invalid data
				amount_total_fee = new BigDecimal(total_fee).divide(new BigDecimal(100)).setScale(2, RoundingMode.UP);		
			} catch (Exception ex) {				
			}
		}
		//支付结果
		String pay_result = resHandler.getParameter("pay_result");
		String orderNo = resHandler.getParameter("sp_billno");
		String buyer_email = resHandler.getParameter("buyer_email");
		
		if( "0".equals(pay_result) ) {
			//------------------------------
			//处理业务开始
			//------------------------------ 
			flag = Constants.PAYMENT_HISTORY_FLAG_SUCCESS; //------------只处理支付-------------
		} else {
			//当做不成功处理
			flag = Constants.PAYMENT_HISTORY_FLAG_FAILURE;
		}
		
		OnlinePaymentResponse onlinePaymentResponse = new OnlinePaymentResponse();
		onlinePaymentResponse.setFlowNo(transaction_id);   //必填. 如果支付网关没有流水号，则使用原始的orderNo，即未decode前的
		onlinePaymentResponse.setOrderNo(orderNo);  //必填
		onlinePaymentResponse.setFlag(flag);    //必填
		onlinePaymentResponse.setAmount(amount_total_fee);  //必填
		onlinePaymentResponse.setPaymentGateway(paymentGateway);  //必填
		onlinePaymentResponse.setPayer(buyer_email);
		OrderService orderService = (OrderService) cxt.getBean("orderService");
		request.setAttribute("onlinePaymentResponse",onlinePaymentResponse);
		orderService.doHandleOnlinePaymentResponse(onlinePaymentResponse,request);
	} else {
		out.println("认证签名失败");
	}

%>
<%
	if( Constants.PAYMENT_HISTORY_FLAG_SUCCESS.equals(flag) ) {
		//调用doShow, 打印meta值跟js代码,告诉财付通处理成功,并在用户浏览器显示$show页面.
		resHandler.doShow(show_url);
	}else{
%>
	<%@include file="./include/finishPayResult.jspf"%>
<%
	}
%>

